﻿
using Microsoft.AspNetCore.Mvc;
using QntPlatform.BinanceApi;
using QntPlatform.Model;
using QntPlatform.Platform;
using QntPlatform;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace QntSvc.Web.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ExchangeController : ApiCtrlBase<ExchangeInfo>
    {

        /// <summary>
        /// 一键平仓
        /// </summary>
        /// <param name="exchangeId"></param>
        /// <param name="Pair">交易对</param>
        /// <param name="isLong">仓位方向</param>
        /// <returns></returns>
        [HttpPost]
        public decimal FastClosing(string exchangeId, string Pair, bool isLong)
        {
            if (string.IsNullOrWhiteSpace(exchangeId)||string.IsNullOrWhiteSpace(Pair))
            {
                throw new ArgumentException("账户id及交易对不能为空");
            }
            var exInfo = GetById(exchangeId);
            if (exInfo==null)
            {
                return -3;
            }
            if (exInfo.PlatformSign == "Binance"||exInfo.PlatformSign == "Binance_USDT")
            {
                var exchange = new BinanceExchangeImp(exInfo.AccessKey, exInfo.SecretKey, Pair);
                var psts = exchange.GetPosition(Pair);
               // var reList = new List<Task<IConvertible>>();
                decimal amount = 0m;
                var drt = DirectionTo.InFor(isLong);
                foreach (var pst in psts)
                {

                    if (pst.Type != drt)
                    {
                        continue;
                    }
                    amount += pst.Amount;
                }
                if (amount==0)
                {
                    return -2;
                }
                if (amount<0 )
                {
                    amount *= -1;
                }
                //Math.Abs()
                var re = exchange.CreateOrder(amount, drt.ToClose(),info:new QntPlatform.Db.LogInfo(){Info2="快速平仓"});
                if ((re.Result + "").Length > 1)
                {
                    return amount;
                }
                else
                {
                    return -1;
                }
            }
            return -3;
        }

        [HttpGet]
        public string TestStr() {
            var key = "sPeY6kbn1def0tt3msPXiqlPUf5U5yatch5U8EjONtQp5BeNKXxkDbonwO3pYSX1";
            var skey = "1bbP3uUaVPIazMVDSOfos1Ji9xbK2561TBmRrTm1WdC0808bZ9KhWrjgbu7nqSAH";
            var log = new RobotLog(0);
           var exchange = new BinanceExchangeImp(key, skey, "BTCUSDT",log);
         var acc=   exchange.GetAccountAsync().Result;
            log.Debug(acc.Balance+",的到余额");
         var re=   exchange.CreateOrder(0.01m, SideDirection.Buy).Result;
            log.Debug(re + ",返回id");

            return acc.Balance + "---" + re;
        }
    }

    [ApiController]
    [Route("api/[controller]/[action]")]
    public abstract class ApiCtrlBase<T>: ControllerBase where T:class,IEntityBase
    {
       protected IMng<T> mng = new QntPlatform.Platform.MngBase<T>();
        [HttpPost]
        public virtual bool Add(T entity)
        {

            return mng.Add(entity);
        }
        [HttpPost]
        public bool Remove(T entity)
        {
            return mng.Remove((IConvertible)entity.GetIds()[0]);
        }
        [HttpPost]
        public bool Edit(T entity)
        {
            return mng.Edit(entity);
        }
        public T[] GetList(T entity)
        {
            return mng.GetList(entity);
        }
        public T GetById([FromQuery]  string id)
        {
            return mng.GetById((IConvertible)id);
        }
    }
}
